package com.yanq.liancar.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.ModifyMailAddressActivity;
import com.yanq.liancar.activity.NewsDetailsActivity;
import com.yanq.liancar.activity.SmallCNewsActivity;
import com.yanq.liancar.adapter.FindAdapter;
import com.yanq.liancar.adapter.MailAddressAdapter;
import com.yanq.liancar.base.BaseLYFragment;
import com.yanq.liancar.bean.BaseBean;
import com.yanq.liancar.bean.FindBean;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.LogUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


@SuppressLint("ValidFragment")
public class SimpleCardFragment extends BaseLYFragment {
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.img_nonews)
    ImageView img_nonews;
    private List<FindBean.DataBean> mItemList = new ArrayList<>();
    private int type;
    private FindAdapter mFindAdapter;
    private int page;
    private boolean isClear;

    public static SimpleCardFragment getInstance(int type) {
        SimpleCardFragment sf = new SimpleCardFragment();
        sf.type = type;
        return sf;
    }

    @Override
    protected void onCreateViewLazy(Bundle savedInstanceState) {
        super.onCreateViewLazy(savedInstanceState);
        setContentView(R.layout.fr_simple_card);


        initRecyclerview();
        initListener();
    }


    @Override
    protected void onResumeLazy() {
        super.onResumeLazy();
        initOkGo();
    }

    private void initRecyclerview() {
        recyclerview.setNestedScrollingEnabled(false);
        recyclerview.setLayoutManager(new LinearLayoutManager(mActivity));
        mFindAdapter = new FindAdapter(R.layout.item_find, mItemList);
        recyclerview.setAdapter(mFindAdapter);

        mFindAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle=new Bundle();
                bundle.putInt("type",1);
                bundle.putInt("newsId",mItemList.get(position).getNewsid());
                RxActivityTool.skipActivity(mActivity, NewsDetailsActivity.class,bundle);
            }
        });

        mFindAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.pinglun:
                        Bundle bundle=new Bundle();
                        bundle.putInt("type",2);
                        bundle.putInt("newsId",mItemList.get(position).getNewsid());
                        RxActivityTool.skipActivity(mActivity, NewsDetailsActivity.class,bundle);
                        break;
                    case R.id.dianzan:
                        initDianzan(mItemList.get(position).getNewsid(),position);
                        break;
                    case R.id.shoucang:
                        initGuanzhu(mItemList.get(position).getNewsid(),position);
                        break;
                }
            }
        });
    }

    private void initListener() {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                isClear = true;


                refreshlayout.finishRefresh();
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                isClear = false;

                refreshLayout.finishLoadMore();
            }

        });
    }

    //新闻列表数据
    private void initOkGo() {
        OkGo.<String>post(Config.s + "llc/api/shareNewsClassify")
                .tag(this)
                .params("newsclassify", type)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        FindBean bean = gson.fromJson(response.body(), FindBean.class);
                        if (bean.getCode()==200){
                            img_nonews.setVisibility(View.GONE);
                            mItemList.clear();
                            mItemList.addAll(bean.getData());
                            mFindAdapter.notifyDataSetChanged();
                        }else if (bean.getCode()==201){
                            img_nonews.setVisibility(View.VISIBLE);
                        }else {
                            RxToast.info(bean.getOutputMessage());
                        }
                    }
                });
    }

    //点赞，取消点赞
    private void initDianzan(int newsId, final int pos) {
        OkGo.<String>post(Config.s + "llc/api/insert_likes")
                .tag(this)
                .params("newsid", newsId+"")
                .params("token", UserBean.getcToken(mActivity))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        BaseBean bean = gson.fromJson(response.body(), BaseBean.class);
                        if (bean.isIsSuccess()){
                           if ("点赞成功".equals(bean.getOutputMessage())){
                               int i=mItemList.get(pos).getNewslikesnum();
                               mItemList.get(pos).setNewslikesnum(i++);
                               FindBean.DataBean dataBean=mItemList.get(pos);
                               dataBean.setNewslikesnum(i);
                               mFindAdapter.setData(pos,dataBean);

                           }else {
                               int i=mItemList.get(pos).getNewslikesnum();
                               mItemList.get(pos).setNewslikesnum(i--);
                               FindBean.DataBean dataBean=mItemList.get(pos);
                               dataBean.setNewslikesnum(i);
                               mFindAdapter.setData(pos,dataBean);
                           }
                        }else {
                            RxToast.error(bean.getOutputMessage());
                        }
                    }
                });
    }

    //关注，取消关注
    private void initGuanzhu(int newsId, final int pos) {
        OkGo.<String>post(Config.s + "llc/api/insert_attention")
                .tag(this)
                .params("newsid", newsId+"")
                .params("token", UserBean.getcToken(mActivity))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        BaseBean bean = gson.fromJson(response.body(), BaseBean.class);
                        if (bean.isIsSuccess()){
                            if ("关注成功".equals(bean.getOutputMessage())){
                                int i=mItemList.get(pos).getNewsattentionnum();
                                mItemList.get(pos).setNewsattentionnum(i++);
                                FindBean.DataBean dataBean=mItemList.get(pos);
                                dataBean.setNewsattentionnum(i);
                                mFindAdapter.setData(pos,dataBean);

                            }else {
                                int i=mItemList.get(pos).getNewsattentionnum();
                                mItemList.get(pos).setNewsattentionnum(i--);
                                FindBean.DataBean dataBean=mItemList.get(pos);
                                dataBean.setNewsattentionnum(i);
                                mFindAdapter.setData(pos,dataBean);
                            }
                        }else {
                            RxToast.error(bean.getOutputMessage());
                        }
                    }
                });
    }
}